package com.copd.mapper;

import com.copd.entity.Role;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface RoleMapper {
    
    @Select("SELECT * FROM Roles")
    List<Role> findAll();
    
    @Select("SELECT * FROM Roles WHERE role_id = #{roleId}")
    Role findById(Integer roleId);
    
    @Select("SELECT * FROM Roles WHERE role_key = #{roleKey}")
    Role findByRoleKey(String roleKey);
    
    @Insert("INSERT INTO Roles(role_name, role_key, description) " +
            "VALUES(#{roleName}, #{roleKey}, #{description})")
    @Options(useGeneratedKeys = true, keyProperty = "roleId")
    int insert(Role role);
    
    @Update("UPDATE Roles SET role_name = #{roleName}, role_key = #{roleKey}, " +
            "description = #{description} WHERE role_id = #{roleId}")
    int update(Role role);
    
    @Delete("DELETE FROM Roles WHERE role_id = #{roleId}")
    int deleteById(Integer roleId);
    
    @Select("SELECT r.* FROM Roles r " +
            "JOIN User_Roles ur ON r.role_id = ur.role_id " +
            "WHERE ur.user_id = #{userId}")
    List<Role> findByUserId(Integer userId);
    
    @Insert("INSERT INTO Role_Permissions(role_id, permission_id) VALUES(#{roleId}, #{permissionId})")
    int addPermissionToRole(@Param("roleId") Integer roleId, @Param("permissionId") Integer permissionId);
    
    @Delete("DELETE FROM Role_Permissions WHERE role_id = #{roleId} AND permission_id = #{permissionId}")
    int removePermissionFromRole(@Param("roleId") Integer roleId, @Param("permissionId") Integer permissionId);
    
    @Delete("DELETE FROM Role_Permissions WHERE role_id = #{roleId}")
    int removeAllPermissionsFromRole(Integer roleId);
    
    @Insert("INSERT INTO User_Roles(user_id, role_id) VALUES(#{userId}, #{roleId})")
    int addRoleToUser(@Param("userId") Integer userId, @Param("roleId") Integer roleId);
    
    @Delete("DELETE FROM User_Roles WHERE user_id = #{userId} AND role_id = #{roleId}")
    int removeRoleFromUser(@Param("userId") Integer userId, @Param("roleId") Integer roleId);
    
    @Delete("DELETE FROM User_Roles WHERE user_id = #{userId}")
    int removeAllRolesFromUser(Integer userId);
    
    @Select("SELECT * FROM Roles WHERE role_name = #{roleName}")
    Role findByName(String roleName);
    
    @Delete("DELETE FROM Role_Permissions WHERE role_id = #{roleId}")
    int deleteRolePermissions(Integer roleId);
    
    @Insert("<script>INSERT INTO Role_Permissions(role_id, permission_id) VALUES " +
            "<foreach collection='permissionIds' item='permissionId' separator=','>" +
            "(#{roleId}, #{permissionId})" +
            "</foreach></script>")
    int insertRolePermissions(@Param("roleId") Integer roleId, @Param("permissionIds") List<Integer> permissionIds);
    
    @Select("SELECT COUNT(*) > 0 FROM User_Roles WHERE user_id = #{userId} AND role_id = #{roleId}")
    boolean checkUserHasRole(@Param("userId") Integer userId, @Param("roleId") Integer roleId);
    
    @Insert("INSERT INTO User_Roles(user_id, role_id) VALUES(#{userId}, #{roleId})")
    int assignRoleToUser(@Param("userId") Integer userId, @Param("roleId") Integer roleId);
} 